package com.ruoyi.hatha.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.hatha.domain.PKecheng;
import com.ruoyi.hatha.service.IPKechengService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.hatha.domain.PHexiaojilu;
import com.ruoyi.hatha.service.IPHexiaojiluService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 核销记录Controller
 * 
 * @author ruoyi
 * @date 2024-04-02
 */
@RestController
@RequestMapping("/hatha/hexiaojilu")
public class PHexiaojiluController extends BaseController
{
    @Autowired
    private IPHexiaojiluService pHexiaojiluService;
    @Autowired
    private IPKechengService kechengService;
    @Autowired
    private ISysUserService userService;

    /**
     * 查询核销记录列表
     */
    @PreAuthorize("@ss.hasPermi('hatha:hexiaojilu:list')")
    @GetMapping("/list")
    public TableDataInfo list(PHexiaojilu pHexiaojilu)
    {
        startPage();
        List<PHexiaojilu> list = pHexiaojiluService.selectPHexiaojiluList(pHexiaojilu);
        return getDataTable(list);
    }

    @GetMapping("/listall")
    public AjaxResult listall(PHexiaojilu pHexiaojilu)
    {
        pHexiaojilu.setUid(SecurityUtils.getUserId());
        List<PHexiaojilu> list = pHexiaojiluService.selectPHexiaojiluList(pHexiaojilu);

        for(int i=0;i<list.size();i++){
            Long kic = list.get(i).getKid();
            Long buyid = list.get(i).getBuyid();

            PKecheng kc = kechengService.selectPKechengById(kic);
            SysUser su = userService.selectUserById(buyid);

            list.get(i).setKecheng(kc);
            list.get(i).setUser(su);

        }

        return AjaxResult.success(list);
    }

    /**
     * 导出核销记录列表
     */
    @PreAuthorize("@ss.hasPermi('hatha:hexiaojilu:export')")
    @Log(title = "核销记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PHexiaojilu pHexiaojilu)
    {
        List<PHexiaojilu> list = pHexiaojiluService.selectPHexiaojiluList(pHexiaojilu);
        ExcelUtil<PHexiaojilu> util = new ExcelUtil<PHexiaojilu>(PHexiaojilu.class);
        util.exportExcel(response, list, "核销记录数据");
    }

    /**
     * 获取核销记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('hatha:hexiaojilu:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(pHexiaojiluService.selectPHexiaojiluById(id));
    }

    /**
     * 新增核销记录
     */
    @PreAuthorize("@ss.hasPermi('hatha:hexiaojilu:add')")
    @Log(title = "核销记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PHexiaojilu pHexiaojilu)
    {
        return toAjax(pHexiaojiluService.insertPHexiaojilu(pHexiaojilu));
    }

    /**
     * 修改核销记录
     */
    @PreAuthorize("@ss.hasPermi('hatha:hexiaojilu:edit')")
    @Log(title = "核销记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PHexiaojilu pHexiaojilu)
    {
        return toAjax(pHexiaojiluService.updatePHexiaojilu(pHexiaojilu));
    }

    /**
     * 删除核销记录
     */
    @PreAuthorize("@ss.hasPermi('hatha:hexiaojilu:remove')")
    @Log(title = "核销记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pHexiaojiluService.deletePHexiaojiluByIds(ids));
    }
}
